﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Ko2.Portal;

namespace KoEventReceiver_Shops_ExList_ListItem.Right
{

    public static class PermDocRight
    {

        #region Members

        static string siteUrl = @"http://localhost/shops/";
        static string listNamePermDocs = "Первичные документы";

        #endregion

        public static Exception SetRoleItem(SPSite parSite, SPWeb parWeb, SPListItem parItem, String parShopName)
        {

            Exception ret = null;
            SPGroup mGroup = null;
            SPList listPermDoc = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        listPermDoc = web.Lists[listNamePermDocs];
                        SPListItem item = listPermDoc.GetItemById(parItem.ID);

                        if (!item.HasUniqueRoleAssignments)
                        {
                            item.BreakRoleInheritance(true);
                        }

                        try
                        {
                            SPRoleAssignmentCollection ImageItemRoles = item.RoleAssignments;
                            while (ImageItemRoles.Count > 0)
                                ImageItemRoles.Remove(0);
                            SPRoleDefinition roleDef = web.RoleDefinitions.GetByType(SPRoleType.Contributor);

                            if (SPSecurityHelper.GetWeGroup(site, parShopName) != null)
                            {
                                mGroup = SPSecurityHelper.GetWeGroup(web, parShopName);
                                SPRoleAssignment RoleAssignment = new SPRoleAssignment(mGroup);
                                RoleAssignment.RoleDefinitionBindings.Add(roleDef);
                                item.RoleAssignments.Add(RoleAssignment);
                            }
                        }
                        catch (Exception err)
                        {
                            ret = new Exception(err.Message, err);
                        }

                        try
                        {
                            SPList list = web.Lists["PermDoc"];
                            if (list != null)
                            {
                                foreach (SPListItem Item in list.Items)
                                {
                                    if (SPSecurityHelper.GetNameGroupByString(Item["Group"].ToString()) != null)
                                    {
                                        if (SPSecurityHelper.GetWeGroup(web, SPSecurityHelper.GetNameGroupByString(Item["Group"].ToString())) != null)
                                        {
                                            mGroup =
                                                SPSecurityHelper.GetWeGroup(
                                                        web
                                                    , SPSecurityHelper.GetNameGroupByString(Item["Group"].ToString()));
                                            SPRoleDefinition rolePerm = null;
                                            //--
                                            string type = Item["Perm"].ToString();
                                            if (type == "Administrator")
                                                rolePerm =
                                                    web.RoleDefinitions.GetByType(SPRoleType.Administrator);
                                            if (type == "Contributor")
                                                rolePerm =
                                                    web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                            if (type == "Reader")
                                                rolePerm =
                                                    web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                            //--
                                            if (rolePerm != null)
                                            {
                                                SPRoleAssignment RoleAssignment = new SPRoleAssignment(mGroup);
                                                RoleAssignment.RoleDefinitionBindings.Add(rolePerm);
                                                item.RoleAssignments.Add(RoleAssignment);
                                            }
                                        } // if (GetWeGroup(GetNameGroupByString(Item["Group"].ToString())) != null)
                                    } // if (GetNameGroupByString(Item["Group"].ToString()) != null)
                                } // foreach (SPListItem Item in list.Items)
                            } // if (list != null)
                        }
                        catch (Exception err)
                        {
                            ret = new Exception(err.Message, err);
                        }
                    }
                }
            });
            return ret;
        }

    }

}
